package com.vlong.town.controller.client.login;

import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.wxUser.WxUser;
import com.vlong.town.service.wxUser.WxUserService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 登录控制器
 * @author 51088
 */
@Controller
@RequestMapping("/client/login")
public class LoginClientController extends BaseController{
	@Autowired
	private  WxUserService  wxUserService;
	
	/**
	 * 跳转登录页面
	 * @return
	 */
	@RequestMapping("/tologin")
	public String toLogin(){
		return "client/login/login";
	}
	
	
	
	/**
	 * 注册
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/register")
	public DataMessage register(
			@RequestParam(required=false, value="telphone") String telphone,
            @RequestParam(required=false, value="passwords") String passwords
			){
		DataMessage  dm=new DataMessage();
		if(!validateStringProperty(telphone, 11, 11)){
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("手机号参数错误");
		}else if(!validateStringProperty(passwords, 6, 8)){
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("手机号参数错误");
		}else{
			List<WxUser> queryList = wxUserService.queryList(null, 1, 1, 1L, null, null, null, null, null, null, null, telphone.trim(), null, null, null, null, null);
			if(queryList!=null&&queryList.size()>0){
				dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
				dm.setMessage("手机号已存在");
			}else{
				wxUserService.add(null, 1, null, null, null, null, null, null, null, null, null, telphone.trim(), null, passwords.trim(), null, null, System.currentTimeMillis(), null);
				super.setTelphone(telphone.trim());//账号（手机号）
				dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			}
		}
		return dm;
	}
	
	
	
	/**
	 * 登录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/executeLogin")
	public DataMessage executeLogin(
			@RequestParam(required=false, value="telphones") String telphones,
            @RequestParam(required=false, value="password") String password
			){
		DataMessage  dm=new DataMessage();
		List<WxUser> queryList = wxUserService.queryList(null, 1, null, null, null, null, null, null, null, null, null, telphones.trim(), null, DataUtil.generateMD5(password.trim().getBytes()), null, null, null);
		if(queryList!=null&&queryList.size()>0){
			super.setTelphone(queryList.get(0).getTelphone());//账号（手机号）
			request.getSession().setAttribute(TownConstant.TOWN_SESSION_USER_TYPE, TownConstant.TOWN_SESSION_USER_TYPE_CLIENT);
			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
			dm.setMessage("登录成功");
		}else{
			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
			dm.setMessage("登录账号或密码不正确");
		}
		return dm;
	} 
	
	/**
	 * 退出
	 */
	@RequestMapping("/loginOut")
	public String loginOut(){
		request.getSession().invalidate();
		return "redirect:/client/index/main";
	}
}
